package com.zcf.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.zcf.common.json.Body;
import com.zcf.common.result.PageResult;
import com.zcf.common.result.ResultVo;
import com.zcf.pojo.Dining;
import com.zcf.pojo.Log;
import com.zcf.pojo.Riding;
import com.zcf.pojo.User;
import com.zcf.service.LogService;
import com.zcf.utils.Hutool;
import com.zcf.vo.in.PageVo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author loupeng
 * @since 2018-12-19
 */
@Controller
@RequestMapping("/log")
@CrossOrigin
@ResponseBody
public class LogController {

	@Autowired
	private LogService logservice;

	@PostMapping("querywdt")
	public Body querywdt(String userId, String addtime) {
		User user = new User();
		user = user.selectById(userId);
		if (user != null) {
			Log log = new Log();
			EntityWrapper<Log> ew = new EntityWrapper<>();
			ew.eq("user_id", userId);
			ew.eq("log_type", 1);
			if (addtime != null) {
				ew.like("addtime", addtime);
			}
			List<Log> li = log.selectList(ew);
			if (li.size() > 0) {
				return Body.newInstance(li);
			}
		}
		return Body.BODY_500;
	}

	/**
	 * 用户消费记录
	 */
	@PostMapping("query")
	public Body query(String addtime, String time) {
		EntityWrapper<Log> ew = new EntityWrapper<>();
		ew.like("log_name", "消费");
		if (addtime != null && addtime != "" && time != null && time != "") {
			ew.between("addtime", addtime, time);
		} else if (addtime != null && addtime != "") {
			ew.like("addtime", addtime);
		} else if (time != null && time != "") {
			ew.like("addtime", time);
		}
		Log log = new Log();
		List<Log> list = log.selectList(ew);
		List<Log> lists = new ArrayList<>();
		for (Log log2 : list) {
			User user = new User();
			user.setId(log2.getUserId());
			user = user.selectById();
			if (user != null) {
				log2.setUserName(user.getName());
			}
			lists.add(log2);
		}
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.BODY_500;
	}

	/**
	 * 用户提现记录
	 */
	@PostMapping("queryTiXian")
	public Body queryTiXian(String addtime, String time) {
		EntityWrapper<Log> ew = new EntityWrapper<>();
		ew.like("log_name", "提现");
		if (addtime != null && addtime != "" && time != null && time != "") {
			ew.between("addtime", addtime, time);
		} else if (addtime != null && addtime != "") {
			ew.like("addtime", addtime);
		} else if (time != null && time != "") {
			ew.like("addtime", time);
		}
		Log log = new Log();
		List<Log> list = log.selectList(ew);
		List<Log> lists = new ArrayList<>();
		for (Log log2 : list) {
			User user = new User();
			user.setId(log2.getUserId());
			user = user.selectById();
			if (user != null) {
				log2.setUserName(user.getName());
			}
			lists.add(log2);
		}
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.BODY_500;
	}

	/**
	 * 商家收入 记录
	 */
	@PostMapping("queryDin")
	public Body queryDin(String addtime, String time, String dinId) {
		EntityWrapper<Log> ew = new EntityWrapper<>();
		ew.eq("log_type", 1);
		if (dinId != null && dinId != "") {
			Dining dining = new Dining();
			EntityWrapper<Dining> ewqa = new EntityWrapper<>();
			ewqa.eq("user_id", dinId);
			dining = dining.selectOne(ewqa);
			if (dining != null) {
				ew.eq("user_id", dining.getUserId());
			}
		}
		if (addtime != null && addtime != "" && time != null && time != "") {
			ew.between("addtime", addtime, time);
		} else if (addtime != null && addtime != "") {
			ew.like("addtime", addtime);
		} else if (time != null && time != "") {
			ew.between("addtime", addtime, time);
		}
		ew.eq("log_desc", "商家收入");
		Log log = new Log();
		List<Log> list = log.selectList(ew);
		List<Log> lists = new ArrayList<>();
		for (Log log2 : list) {
			User user = new User();
			user.setId(log2.getUserId());
			user = user.selectById();
			if (user != null) {
				log2.setUserName(user.getName());
			}
			lists.add(log2);
		}
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.BODY_500;
	}

	/**
	 * 骑手收入记录
	 */
	@PostMapping("queryRin")
	public Body queryRin(String addtime, String time) {
		EntityWrapper<Log> ew = new EntityWrapper<>();
		ew.eq("log_type", 1);
		if (addtime != null && addtime != "" && time != null && time != "") {
			ew.between("addtime", addtime, time);
		} else if (addtime != null && addtime != "") {
			ew.like("addtime", addtime);
		} else if (time != null && time != "") {
			ew.like("addtime", time);
		}
		ew.eq("log_desc", "骑手收入");
		Log log = new Log();
		List<Log> list = log.selectList(ew);
		List<Log> lists = new ArrayList<>();
		for (Log log2 : list) {
			User user = new User();
			user.setId(log2.getUserId());
			user = user.selectById();
			if (user != null) {
				log2.setUserName(user.getName());
			}
			lists.add(log2);
		}
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.BODY_500;
	}

	/**
	 * 骑手收入记录
	 */
	@PostMapping("queryDai")
	public Body queryDai(String addtime, String time) {
		EntityWrapper<Log> ew = new EntityWrapper<>();
		ew.eq("log_type", 1);
		if (addtime != null && addtime != "" && time != null && time != "") {
			ew.between("addtime", addtime, time);
		} else if (addtime != null && addtime != "") {
			ew.like("addtime", addtime);
		} else if (time != null && time != "") {
			ew.like("addtime", time);
		}
		ew.eq("log_desc", "代理收入");
		Log log = new Log();
		List<Log> list = log.selectList(ew);
		List<Log> lists = new ArrayList<>();
		for (Log log2 : list) {
			User user = new User();
			user.setId(log2.getUserId());
			user = user.selectById();
			if (user != null) {
				log2.setUserName(user.getName());
			}
			lists.add(log2);
		}
		if (lists.size() > 0) {
			return Body.newInstance(lists);
		}
		return Body.BODY_500;
	}

	@PostMapping("delete")
	public Body updaterefuseStatus(String id) {
		String[] appid = id.split(",");
		Boolean flag = false;
		Log din = new Log();
		for (int i = 0; i < appid.length; i++) {
			din.setLogId(appid[i]);
			flag = din.deleteById();
		}
		if (flag == true) {
			return Body.newInstance("删除成功");
		}
		return Body.newInstance("删除失败");
	}

	/**
	 * 骑手查询奖励
	 */
	@PostMapping("addAward")
	private Body addAward(PageVo pv, String userId) {
		Log log = new Log();
		EntityWrapper<Log> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		ew.eq("log_name", "获得五星好评一次");
		if (pv.getPage() != null && pv.getPageSize() != null) {
			PageHelper.startPage(pv.getPage(), pv.getPageSize());
		}
		PageResult pr = PageResult.result(log.selectList(ew));
		ResultVo rv = new ResultVo();
		rv.setList(pr.getRows());
		rv.setTotal(pr.getTotal());
		if (rv != null) {
			return Body.newInstance(rv);
		}
		return Body.BODY_500;
	}

	/**
	 * 超时提醒
	 * 
	 * @param userId
	 * @return
	 */
	@PostMapping("addOvertime")
	private Body addOvertime(PageVo pv, String userId) {
		Log log = new Log();
		EntityWrapper<Log> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		ew.eq("log_name", "超时提醒");
		if (pv.getPage() != null && pv.getPageSize() != null) {
			PageHelper.startPage(pv.getPage(), pv.getPageSize());
		}
		PageResult pr = PageResult.result(log.selectList(ew));
		ResultVo rv = new ResultVo();
		rv.setList(pr.getRows());
		rv.setTotal(pr.getTotal());
		if (rv != null) {
			return Body.newInstance(rv);
		}
		return Body.BODY_500;
	}

	/**
	 * 超时提醒 查看未查看
	 */
	@PostMapping("addCounts")
	public Body addCounts(String logId) {
		String[] appid = logId.split(",");
		Log user = new Log();
		Boolean falg = false;
		for (int i = 0; i < appid.length; i++) {
			user.setLogId(appid[i]);
			user.setCounts(1);
			falg = user.updateById();
		}
		if (falg == true) {
			return Body.newInstance("修改成功");
		}
		return Body.newInstance("修改失败");
	}

	/**
	 * 骑手惩罚
	 * 
	 * @param userId
	 */
	@PostMapping("addPunishment")
	private Body addPunishment(PageVo pv, String userId) {
		ResultVo rv = logservice.addPunishment(pv, userId);
		if (rv != null) {
			return Body.newInstance(rv);
		}
		return Body.BODY_500;
	}

	/**
	 * 后台查询----骑手收入
	 */
	@PostMapping("addRiding")
	public Body addRiding(String rinName, String startTime, String finishTime) {
		EntityWrapper<Log> ew = new EntityWrapper<>();
		ew.eq("log_desc", "骑手收入");
		if (rinName != null && rinName != "") {
			ew.like("log_name", rinName);
		}
		if (startTime != null && startTime != "" && finishTime != null && finishTime != "") {
			ew.between("addtime", startTime, finishTime);
		} else if (startTime != null && startTime != "") {
			ew.eq("addtime", startTime);
		} else if (finishTime != null && finishTime != "") {
			ew.eq("addtime", finishTime);
		}
		Log log = new Log();
		List<Log> list = log.selectList(ew);
		List<Log> addlist = new ArrayList<>();
		for (Log log2 : list) {
			User user = new User();
			user = user.selectById(log2.getUserId());
			if (user != null) {
				log2.setUserName(user.getName());
				addlist.add(log2);
			}
		}
		if (addlist.size() > 0) {
			return Body.newInstance(list);
		}
		return Body.BODY_500;
	}
}
